package core.generator.service.impl;

import core.config.exception.CloudException;
import core.generator.pojo.constant.OutFilePath;
import core.generator.pojo.constant.TemplatePath;
import core.generator.service.GeneratorService;
import core.utils.FileUtils;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @author Long
 * @version 1.0
 * @description:
 * @title GeneratorServiceImpl
 * @date 2021/3/6 10:27
 */
public class GeneratorServiceImpl implements GeneratorService {
    @Override
    public void codeGenerator(Map<String, Object> data, String templatePath, String outPath) {
        templatePath = this.getTemplatePath(templatePath);
        Configuration cfg = this.getConfiguration(templatePath);
        try {
            List<File> fileList = FileUtils.searchAllFile(new File(templatePath));
            for (File file : fileList) {
                String templateFileName = file.getAbsolutePath().replace(templatePath, "");
                String outFileName = this.getOutFileName(data, templateFileName, cfg);
                File file1 = FileUtils.mkdir(outPath, outFileName);
                //4.模板处理（文件生成）
                FileWriter fw = new FileWriter(file1);
                this.getTemplate(templateFileName, cfg).process(data, fw);
                fw.close();
            }
        } catch (IOException | TemplateException e) {
            throw new CloudException("代码生成异常");
        }
    }

    @Override
    public void codeGenerator(Map<String, Object> data, String outPath) {
        this.codeGenerator(data, TemplatePath.DEFAULT, outPath);
    }

    @Override
    public void codeGenerator(Map<String, Object> data) {
        this.codeGenerator(data, TemplatePath.DEFAULT, OutFilePath.DEFAULT);
    }

    @Override
    public Configuration getConfiguration(String templatePath) {
        try {
            Configuration cfg = new Configuration();
            FileTemplateLoader ftl = new FileTemplateLoader(new File(templatePath));
            cfg.setTemplateLoader(ftl);
            return cfg;
        } catch (IOException e) {
            throw new CloudException("模板配置加载异常");
        }
    }

    @Override
    public Template getTemplate(String templateFileName, Configuration configuration) {
        try {
            Template template = configuration.getTemplate(templateFileName);
            //指定生成文件的字符集编码
            template.setOutputEncoding("utf-8");
            return template;
        } catch (IOException e) {
            throw new CloudException("模板加载异常");
        }
    }

    @Override
    public String getTemplatePath(String templatePath) {
        return  new File(this.getClass().getResource("/code/templates/" + templatePath).getPath()).getAbsolutePath();
    }

    @Override
    public String getOutFileName(Map<String, Object> data, String templateFileName, Configuration configuration) {
        try {
            Writer writer = new StringWriter();
            Template template = new Template("ts", new StringReader(templateFileName), configuration);
            template.process(data, writer);
            return writer.toString();
        } catch (IOException | TemplateException e) {
            throw new CloudException("输出流异常");
        }
    }
}
